<?php

namespace App\Http\Controllers\admin;

use JohnLui\AliyunOSS;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

//主页
class UploadController extends Controller
{
	//测试上传图片

	// index页
	public function index(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg','gif'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('type',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('type')->put($filename, file_get_contents($path));
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	// index页
	public function shop_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg','gif'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('shop',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('shop')->put($filename, file_get_contents($path));
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	// index页
	public function logo_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg','gif'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('logo',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('logo')->put($filename, file_get_contents($path));
//			$filename = "/upload/logo/" . $filename;
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	// index页
	public function label_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('home',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('home')->put($filename, file_get_contents($path));
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//上传微信二维码
	public function qrcode_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('home',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('qrcode')->put($filename, file_get_contents($path));
//			$filename = "/upload/qrcode/" . $filename;
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//上传资源封面
	public function source_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg','gif'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('source',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('source')->put($filename, file_get_contents($path));
//			$filename = "/upload/source/" . $filename;
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//banner
	public function banner_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('banner',$request->file);
//			$ext = $request->file('banner')->getClientOriginalExtension();
			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('banner')->put($filename, file_get_contents($path));
//			$filename = "/upload/banner/" . $filename;
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//添加商品富文本编辑器上传图片
	public function shop_editor()
	{
		$img = upload('file', 'shop');
		if ($img['code'] == 1) {
			return '{"code":0,"msg":"成功上传","data":{"src":"' . $img['data'] . '"}}';
		} else {
			$arr['code'] = 1;
			$arr['msg'] = '上传失败';
			return json($arr);
		}
	}

	//上传资源封面
	public function goods_up(Request $request)
	{
		if ($request->hasFile('file') && $request->file('file')->isValid()) {
			$allow_types = ['png', 'jpeg', 'jpg'];
			if (!in_array($request->file('file')->getClientOriginalExtension(), $allow_types)) {
				return returnJson(0, '图片格式不支持');
			}
			$picname = $this->common_up('goodpic',$request->file);
//			$ext = $request->file('file')->getClientOriginalExtension();
//			//获取文件的绝对路径
//			$path = $request->file('file')->getRealPath();
//			//定义文件名
//			$filename = date('Ymdhis') . '-' . rand(1000, 9999) . "." . $ext;
//			//存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
//			$shangchuan = Storage::disk('goodpic')->put($filename, file_get_contents($path));
//			$a = $filename;
//			$filename = "/upload/goodpic/" . $filename;
			if ($picname) {
				return ['code' => 1, 'msg' => '图片上传成功', 'info' =>$picname, 'data' => $picname];
			} else {
				return returnJson(0, '图片上传失败');
			}
		} else {
			return returnJson(0, '图片上传失败');
		}
	}

	//公共上传图片方法
	public function common_up($path,$file,$drive='oss'){
		$disk = Storage::disk($drive);
		//将图片上传到OSS中，并返回图片路径信息 值如:avatar/WsH9mBklpAQUBQB4mL.jpeg
		$path = $disk->put($path, $file);
		//由于图片不在本地，所以我们应该获取图片的完整路径，
		//值如：https://test.oss-cn-hongkong.aliyuncs.com/avatar/8GdIcz1NaCZ.jpeg
		return $disk->url($path);

	}


}
